package com.ruoyi.cs.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.cs.domain.TGoodsPool;
import com.ruoyi.cs.mapper.TGoodsPoolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.TTaskReceiveMapper;
import com.ruoyi.cs.domain.TTaskReceive;
import com.ruoyi.cs.service.ITTaskReceiveService;

/**
 * 用户任务领取记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-02
 */
@Service
@SuppressWarnings("all")
public class TTaskReceiveServiceImpl implements ITTaskReceiveService 
{
    @Autowired
    private TTaskReceiveMapper tTaskReceiveMapper;
    @Autowired
    private TGoodsPoolMapper tGoodsPoolMapper;

    /**
     * 查询用户任务领取记录
     * 
     * @param id 用户任务领取记录主键
     * @return 用户任务领取记录
     */
    @Override
    public TTaskReceive selectTTaskReceiveById(Long id)
    {
        return tTaskReceiveMapper.selectTTaskReceiveById(id);
    }

    /**
     * 查询用户任务领取记录列表
     * 
     * @param tTaskReceive 用户任务领取记录
     * @return 用户任务领取记录
     */
    @Override
    public List<TTaskReceive> selectTTaskReceiveList(TTaskReceive tTaskReceive)
    {
        List<TTaskReceive> list = tTaskReceiveMapper.selectTTaskReceiveList(tTaskReceive);
        if(CollectionUtil.isNotEmpty(list)){
            List<String> goodsIds = list.stream().filter(bean -> bean.getRewardType() == 1).distinct().map(TTaskReceive::getGoodsPoolId).collect(Collectors.toList());
            List<TGoodsPool> tGoodsPools = tGoodsPoolMapper.batchQueryById(goodsIds);
            Map<String, TGoodsPool> map = tGoodsPools.stream().collect(Collectors.toMap(TGoodsPool::getGoodsId, goodsPool -> goodsPool));

            list.forEach(task -> {
                if( task.getRewardType() == 1 && map.get(task.getGoodsPoolId()) != null){
                    TGoodsPool tGoodsPool = map.get(task.getGoodsPoolId());
                    task.setGoodsName(tGoodsPool.getGoodsName());
                    task.setGoodsPic(tGoodsPool.getGoodsPic());
                    task.setGoodsPrice(tGoodsPool.getGoodsPrice());
                }
            });
        }
        return list;
    }

    /**
     * 新增用户任务领取记录
     * 
     * @param tTaskReceive 用户任务领取记录
     * @return 结果
     */
    @Override
    public int insertTTaskReceive(TTaskReceive tTaskReceive)
    {
        tTaskReceive.setCreateTime(DateUtils.getNowDate());
        return tTaskReceiveMapper.insertTTaskReceive(tTaskReceive);
    }

    /**
     * 修改用户任务领取记录
     * 
     * @param tTaskReceive 用户任务领取记录
     * @return 结果
     */
    @Override
    public int updateTTaskReceive(TTaskReceive tTaskReceive)
    {
        return tTaskReceiveMapper.updateTTaskReceive(tTaskReceive);
    }

    /**
     * 批量删除用户任务领取记录
     * 
     * @param ids 需要删除的用户任务领取记录主键
     * @return 结果
     */
    @Override
    public int deleteTTaskReceiveByIds(Long[] ids)
    {
        return tTaskReceiveMapper.deleteTTaskReceiveByIds(ids);
    }

    /**
     * 删除用户任务领取记录信息
     * 
     * @param id 用户任务领取记录主键
     * @return 结果
     */
    @Override
    public int deleteTTaskReceiveById(Long id)
    {
        return tTaskReceiveMapper.deleteTTaskReceiveById(id);
    }
}
